#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../XxxRingBuffer.h"



STR_XxxRingBuffer* m_pRBuf;					/**< 环形缓冲区对象 */
unsigned char m_buf[10];					/**< 环形缓冲区的数据存储内存 */
unsigned char m_writeBuf[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};	/**< 写入数据 */
unsigned char m_readBuf[10] = {0};			/**< 用于保存(仅)读的数据 */
unsigned char m_pickBuf[10] = {0};			/**< 用于保存提取的数据 */
int m_abandonedNum = 0;						/**< 插入过程中被抛弃的数据长度 */
int m_validNum = 0;							/**< 实际有效数据长度 */
int m_pickNum = 0;							/**< 实际提取到的长度 */



void Debug_Buffer(void)
{
	printf("%-15s","m_buf");
	for(unsigned int i = 0; i < sizeof(m_buf); i++)
	{
		printf("%-3d ", m_buf[i]);
	}
	printf("\n");

	printf("%-15s","m_readBuf");
	for(unsigned int i = 0; i < sizeof(m_readBuf); i++)
	{
		if(0 == m_readBuf[i])continue;

		printf("%-3d ", m_readBuf[i]);
	}
	printf("\n");

	printf("%-15s","m_pickBuf");
	for(unsigned int i = 0; i < sizeof(m_pickBuf); i++)
	{
		if(0 == m_pickBuf[i])continue;

		printf("%-3d ", m_pickBuf[i]);
	}
	printf("\n");

	printf("本次抛弃数据长度：%d\n", m_abandonedNum);
	printf("剩余有效数据长度：%d\n", m_validNum);
	printf("实际取出数据长度：%d\n", m_pickNum);
	printf("剩余多少有效空间：%d\n",XxxRingBuffer_HowManyIdle(m_pRBuf));	/* 测试读有效空闲位置函数 */
	printf("\n\n");

	memset(m_readBuf, 0, sizeof(m_readBuf));
	memset(m_pickBuf, 0, sizeof(m_pickBuf));
	
	m_abandonedNum = 0;
	m_validNum = 0;
	m_pickNum = 0;
}



int main(void)
{
	int tempFlag;
	
	printf("\n\nXxxRingBuffer test start\n\n");

	m_pRBuf = (STR_XxxRingBuffer*)malloc(XxxRingBuffer_GetSize());		/* 给环形缓冲区对象申请空间 */
	if(NULL == m_pRBuf)
	{
		printf("申请堆空间失败\n");
		return 0;
	}

	tempFlag = XxxRingBuffer_Register(m_pRBuf, m_buf, sizeof(m_buf));	/* 注册环形缓冲区 */
	if(0 != tempFlag)
	{
		printf("注册失败[%d]\n", tempFlag);
	}
	else
	{
		printf("注册成功\n");
		Debug_Buffer();
	}

	printf("-----------------------------------------------------\n");
	printf("· 尾插&&只读全数据测试\n");
	printf("·· 尾插1,2,3,4,5\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);			/* 测试只读全部数据函数 */
	Debug_Buffer();

	printf("·· 尾插6,7,8,9,10 (验证最大有效长度为缓冲区长度-1)\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+5, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插11,12,13,14,15\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+10, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插16,17,18,19,20\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+15, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("-----------------------------------------------------\n");
	printf("· 清空(复位)测试\n");
	XxxRingBuffer_Empty(m_pRBuf);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();
	printf("·· 尾插1,2,3,4,5\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+5, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插11,12,13,14,15\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+10, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插16,17,18,19,20\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+15, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("-----------------------------------------------------\n");
	printf("· 头插测试\n");
	printf("·· 清空\n");
	XxxRingBuffer_Empty(m_pRBuf);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 头插1,2,3,4,5\n");
	m_abandonedNum = XxxRingBuffer_InsertHead(m_pRBuf, m_writeBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 头插6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertHead(m_pRBuf, m_writeBuf+5, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 头插11,12,13,14,15\n");
	m_abandonedNum = XxxRingBuffer_InsertHead(m_pRBuf, m_writeBuf+10, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 头插16,17,18,19,20\n");
	m_abandonedNum = XxxRingBuffer_InsertHead(m_pRBuf, m_writeBuf+15, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("-----------------------------------------------------\n");
	printf("· 提取数据(从头部提取)测试\n");
	printf("·· 清空\n");
	XxxRingBuffer_Empty(m_pRBuf);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();
	printf("·· 从头部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+5, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5,6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 10);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5,6,7,8,9,10,11,12,13,14,15\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 15);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从头部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpHead(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("-----------------------------------------------------\n");
	printf("· 提取数据(从尾部提取)测试\n");
	printf("·· 清空\n");
	XxxRingBuffer_Empty(m_pRBuf);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();
	printf("·· 从尾部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf+5, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5,6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 10);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5,6,7,8,9,10,11,12,13,14,15\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 15);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 从尾部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();
	
	printf("·· 从尾部想再提取5个\n");
	m_pickNum = XxxRingBuffer_PickUpTail(m_pRBuf, m_pickBuf, 5);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("-----------------------------------------------------\n");
	printf("· 只读指定长度数据测试\n");
	printf("·· 清空\n");
	XxxRingBuffer_Empty(m_pRBuf);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	printf("·· 尾插1,2,3,4,5,6,7,8,9,10\n");
	m_abandonedNum = XxxRingBuffer_InsertTail(m_pRBuf, m_writeBuf, 10);
	m_validNum = XxxRingBuffer_OnlyReadHead(m_pRBuf, m_readBuf);
	Debug_Buffer();

	unsigned char tempBuf[10];
	int tempReadNum;
	printf("·· 偏移0, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 0, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移1, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 1, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移2, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 2, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移5, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 5, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移8, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 8, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移9, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 9, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移10, 读5长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 10, 5);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移0, 读9长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 0, 9);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移0, 读10长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 0, 10);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

	printf("·· 偏移5, 读10长度\n");
	memset(tempBuf, 0, sizeof(tempBuf));
	tempReadNum = XxxRingBuffer_OnlyReadHead_Len(m_pRBuf, tempBuf, 5, 10);
	printf("%-15s","tempBuf");
	for(unsigned int i = 0; i < sizeof(tempBuf); i++)
	{
		if(0 == tempBuf[i])continue;

		printf("%-3d ", tempBuf[i]);
	}
	printf("\n");
	printf("实际读到的数据长度：%d\n", tempReadNum);

    return 0;
}
